package Demo3;

import java.util.*;

public class Test {
    public List<String> topKFrequent(String[] words, int k) {
        //1、统计每个单词出现的次数
        Map<String, Integer> map = new
                HashMap<>();
        for (String word : words) {
            if (map.get(word) == null) {
                map.put(word, 1);
            } else {
                int val = map.get(word);
                map.put(word, val + 1);
            }
        }

        //2、建小根堆，指定比较的方式
        PriorityQueue<Map.Entry<String, Integer>> minHeap = new PriorityQueue<>
                //匿名内部类
                (new Comparator<Map.Entry<String, Integer>>() {
                    @Override
                    public int compare(Map.Entry<String, Integer> o1, Map.Entry<String, Integer> o2) {
                        if (o1.getValue().equals(o2.getValue())){
                            //按照字母顺序建立大根堆
                            return o2.getKey().compareTo(o1.getKey());
                        }
                        return o1.getValue() - o2.getValue();
                    }
                });
        //遍历map  调整优先级队列
        for (Map.Entry<String, Integer> entry: map.entrySet()) {
            if(minHeap.size() < k){
                minHeap.offer(entry);
            }else{
                Map.Entry<String, Integer> top = minHeap.peek();
                //如果当前频率相同
                if(top.getValue().compareTo(entry.getValue())==0){
                    //字母顺序小的进来
                    if(top.getKey().compareTo(entry.getKey()) > 0){
                        //出队
                        minHeap.poll();
                        minHeap.offer(entry);
                    }
                }else {
                    //说明出现了更高频率的词
                    if(top.getValue().compareTo(entry.getValue()) < 0){
                        minHeap.poll();
                        minHeap.offer(entry);
                    }
                }
            }
        }
        List<String> ret = new ArrayList<>();
        for (int i = 0; i < k; i++) {
            Map.Entry<String, Integer> top = minHeap.poll();
            ret.add(top.getKey());
        }
        Collections.reverse(ret);
        return ret;
    }
    //统计单词出现的次数
    public static void func(String[] words){
        Map<String,Integer> map = new HashMap<>();
        for (String word : words) {
            if(map.get(word) == null){
                map.put(word,1);
            }else{
                int val = map.get(word);
                map.put(word,val+1);
            }
        }
        System.out.println(map);
    }

    public static void main(String[] args) {
        String[] words = {"this","this","man","man","man","man","manba"};
        func(words);
    }
    public static void main2(String[] args) {
        HushBuck2<Person,String> hushBuck2 = new HushBuck2<>();
        Person person1 = new Person("123");
        Person person2 = new Person("123");
        hushBuck2.put(person1, "777");
        System.out.println(hushBuck2.get(person1));//777
        hushBuck2.put(person2, person2.id);
        System.out.println(hushBuck2.get(person1));//123
        System.out.println(hushBuck2.get(person2));//123
    }
    public static void main1(String[] args) {
        Person person1 = new Person("123");
        Person person2 = new Person("123");
        System.out.println(person1.hashCode());//将对象转变为一个整数
        System.out.println(person1.hashCode());//将对象转变为一个整数
    }
}
